package dev.gtealib.com.opencv;

import android.graphics.Bitmap;

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

/**
 * Created by liusp on 2018/1/17.
 */

public class OpenCVTools {

    static {
        System.loadLibrary("opencv_java3");
    }

    //变灰
    static Bitmap Gray(Bitmap src){
        int width = src.getWidth();
        int height = src.getHeight();

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_gray = new Mat(width, height, CvType.CV_8UC1);
        Imgproc.cvtColor(mat_src, mat_gray, Imgproc.COLOR_BGRA2GRAY, 1);

        Bitmap dest = Bitmap.createBitmap(mat_gray.cols(), mat_gray.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_gray, dest);
        return dest;
    }

    //锐化
    static Bitmap Sharpen(Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();

        Mat kernel = new Mat(3, 3, CvType.CV_16SC1);
        kernel.put(0, 0, 0, -1, 0, -1, 5, -1, 0, -1, 0);

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_dst = new Mat(width, height, CvType.CV_8UC4);
        Imgproc.filter2D(mat_src,mat_dst,mat_src.depth(),kernel);

        Bitmap dest = Bitmap.createBitmap(mat_dst.cols(), mat_dst.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_dst, dest);
        return dest;
    }

    //对比度、亮度
    static Bitmap Contrast(Bitmap src,double alpha) {
        int width = src.getWidth();
        int height = src.getHeight();

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_dst = new Mat(width, height, CvType.CV_8UC4);
        mat_src.convertTo(mat_dst,-1, alpha, 1);

        Bitmap dest = Bitmap.createBitmap(mat_dst.cols(), mat_dst.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_dst, dest);
        return dest;
    }

    //亮度
    static Bitmap Brightness(Bitmap src,double beta) {
        int width = src.getWidth();
        int height = src.getHeight();

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_dst = new Mat(width, height, CvType.CV_8UC4);
        mat_src.convertTo(mat_dst,-1, 1, beta);

        Bitmap dest = Bitmap.createBitmap(mat_dst.cols(), mat_dst.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_dst, dest);
        return dest;
    }

    //中值滤波
    static Bitmap MedianBlur(Bitmap src, int ksize){
        int width = src.getWidth();
        int height = src.getHeight();

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_dst = new Mat(width, height, CvType.CV_8UC4);

        Imgproc.medianBlur(mat_src,mat_dst, ksize);

        Bitmap dest = Bitmap.createBitmap(mat_dst.cols(), mat_dst.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_dst, dest);
        return dest;
    }

    //高斯滤波
    static Bitmap GaussianBlur(Bitmap src, Size ksize, double sigmaX){
        int width = src.getWidth();
        int height = src.getHeight();

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_dst = new Mat(width, height, CvType.CV_8UC4);

        Imgproc.GaussianBlur(mat_src,mat_dst, ksize, sigmaX);

        Bitmap dest = Bitmap.createBitmap(mat_dst.cols(), mat_dst.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_dst, dest);
        return dest;
    }

    static Bitmap Threshold(Bitmap src, double thresh, double maxval) {
        int width = src.getWidth();
        int height = src.getHeight();

        Mat mat_src = new Mat(width, height, CvType.CV_8UC4);
        Utils.bitmapToMat(src, mat_src);

        Mat mat_dst = new Mat(width, height, CvType.CV_8UC4);

        //Imgproc.CV_THRESH_BINARY
        //CV_THRESH_OTSU
        Imgproc.threshold(mat_src,mat_dst,thresh, maxval,0);

        Bitmap dest = Bitmap.createBitmap(mat_dst.cols(), mat_dst.rows(), Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap(mat_dst, dest);
        return dest;
    }

}
